package com.gitee.cashzhang27.test.cloud.webflux.hi.service;

import java.time.Duration;
import java.util.Map;
import java.util.function.Supplier;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * @author Cash Zhang
 * @version v1.0
 * @since 2019/11/26 10:33
 */
@Service
public class HttpBinService {

  private WebClient rest;

  public HttpBinService(@Qualifier("webClientBuilder") WebClient.Builder builder) {
    this.rest = builder.baseUrl("http://httpbin.org").build();
  }

  public Mono<Map> get() {
    return rest.get().uri("/get").retrieve().bodyToMono(Map.class);
  }

  public Mono<Map> delay(int seconds) {
    return rest.get().uri("/delay/{seconds}", seconds).retrieve().bodyToMono(Map.class);
  }

  public Supplier<Mono<Map>> delaySuppplier(int seconds) {
    return () -> this.delay(seconds);
  }

  public Flux<String> fluxDelay(int seconds) {
    return Flux.just("1", "2", "3").delayElements(Duration.ofSeconds(seconds));
  }
}
